package com.alibaba.citrus.ots.unified.inventory.functions.allocateOrderDetail;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import com.alibaba.citrus.ots.common.util.JsonUtil;

import com.epoch.app.otsunifiedinventory.dto.request.AllocateOrderSyncRequest;
import com.epoch.app.otsunifiedinventory.model.dto.AllocateOrderCreateRequest;
import com.epoch.app.otsunifiedinventory.model.dto.AllocateOrderScItemDetailDTO;
import com.epoch.app.otsunifiedinventory.sdo.AllocateOrderDetailSDO;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;

/**
 * @description:
 * @author: yuhw
 * @date: 2021年09月18日 14:51
 **/
public class AllocateOrderDetailFactory {

    public static List<AllocateOrderDetailEntity> create(AllocateOrderCreateRequest allocateOrderSyncRequest){
        List<AllocateOrderDetailEntity> entities = Lists.newArrayList();
//        for (AllocateOrderScItemDetailDTO allocateOrderScItemDetailDTO :
//                allocateOrderSyncRequest.getAllocateOrderScItemDetails()){
//            AllocateOrderDetailSDO detailSDO = new AllocateOrderDetailSDO();
//            detailSDO = convertAllocateOrderDetailSDO(allocateOrderScItemDetailDTO);
//            AllocateOrderDetailEntity entity = AllocateOrderDetailEntity.of(detailSDO);
//            entities.add(entity);
//        }
        if (CollectionUtils.isEmpty(allocateOrderSyncRequest.getAllocateOrderScItemDetails())){
            return entities;
        }
        for (int i =0; i< allocateOrderSyncRequest.getAllocateOrderScItemDetails().size();i++){
            AllocateOrderScItemDetailDTO allocateOrderScItemDetailDTO = allocateOrderSyncRequest.getAllocateOrderScItemDetails().get(i);
            AllocateOrderDetailSDO detailSDO = new AllocateOrderDetailSDO();
            detailSDO = convertAllocateOrderDetailSDO(allocateOrderScItemDetailDTO);
            detailSDO.setType(allocateOrderSyncRequest.getType());
            detailSDO.setLineNo(String.valueOf(i +1));
            AllocateOrderDetailEntity entity = AllocateOrderDetailEntity.of(detailSDO);
            entities.add(entity);
        }
        return entities;
    }

    /**
     * 根据F-F1
     * @param allocateOrderSyncRequest
     * @return
     */
    public static List<AllocateOrderDetailEntity> create(AllocateOrderSyncRequest allocateOrderSyncRequest){
        List<AllocateOrderDetailEntity> entities = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(allocateOrderSyncRequest.getAllocateOrderScItemDetails())){
            for (AllocateOrderScItemDetailDTO allocateOrderScItemDetailDTO :
                    allocateOrderSyncRequest.getAllocateOrderScItemDetails()){
                AllocateOrderDetailSDO detailSDO = convertAllocateOrderDetailSDO(allocateOrderScItemDetailDTO);
                detailSDO.setType(allocateOrderSyncRequest.getType());
                AllocateOrderDetailEntity entity = AllocateOrderDetailEntity.of(detailSDO);
                entities.add(entity);
            }
        }
        return entities;
    }

    public static AllocateOrderDetailSDO convertAllocateOrderDetailSDO(AllocateOrderScItemDetailDTO dto){
        AllocateOrderDetailSDO sdo = JsonUtil.convert(dto,AllocateOrderDetailSDO.class);
//        sdo.setAllocateOrderId(getLongValue(dto.getAllocateOrderId()));
//        sdo.setApplyQuantity(getLongValue(dto.getApplyQuantity()));
//        sdo.setAllocateApplyOrderLineId(dto.getAllocateApplyOrderLineNo());
//        sdo.setCloseReason(dto.getClosedReason());
//        sdo.setCreator(dto.getCreator());
//        sdo.setConfirmQuantity(dto.getConfirmQuantity());
//        sdo.setOutAllocateOrderLineId(dto.getOutAllocateOrderLineNo());
////        sdo.setAllocateOrderId();
//        sdo.setExpectedProduceDate(dto.getExpectedProduceDate());
//        sdo.setPrice(getLongValue(dto.getUnitPrice()));
//        sdo.setStatus(dto.getStatus());
//        sdo.setOutAllocateOrderLineId(dto.getOutAllocateOrderLineNo());
//        sdo.setUnit(dto.getUnit());
//        sdo.setScItemCode(dto.getScItemCode());
//        sdo.setReplyQuantity(getLongValue(dto.getReplyQuantity()));
//        sdo.setProductGroup((dto.getProductGroup()));
//        sdo.setOutAllocateOrderLineId(dto.getOutAllocateOrderLineId());
//        sdo.setApplyQuantity(getLongValue(dto.getApplyQuantity()));
//        sdo.setScItemId(dto.getScItemId());
//        sdo.setQuantity(dto.getQuantity());
//        sdo.setLineNo(dto.getLineNo());
//        sdo.setClose(dto.getClosed());
//        sdo.setType(dto.getType());
//        sdo.setDiscountAmount(dto.getSubTotalAmount());
//        sdo.setCloseReason(dto.getClosedReason());
//        //sdo.setRemarks(dto.getRemarks());
//        sdo.setGiftQuantity(dto.getGiftQuantity());
        //sdo.setTaxRate(dto.getTexRate());
        return sdo;
    }

    public static Integer getIntValue(String obj){
        return Optional.ofNullable(obj).map(Integer::parseInt).orElse(null);
    }
    public static Long getLongValue(String obj){
        if (Objects.isNull(obj)||"".equals(obj)){
            obj = null;
        }
        return Optional.ofNullable(obj).map(Long::parseLong).orElse(null);
    }

}
